package com.hyd.fragment;

import com.hyd.project.PictureBookListActivity;
import com.hyd.project.R;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

public class PictureBookFragment extends Fragment implements SwipeRefreshLayout.OnRefreshListener,OnClickListener{
	private SwipeRefreshLayout refreshLayout;
	private LinearLayout application_gallery,style_gallery,area_gallery;
	private LayoutInflater inflater;
	private RelativeLayout realPicture;
	private Handler hander = new Handler() {
		public void handleMessage(android.os.Message msg) {
			if (msg.what == 1) {
				refreshLayout.setRefreshing(false);
			}
		};
	};

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		return inflater.inflate(R.layout.picture_book_fragment, null);
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);

		InitView();
		InitData();
	}

	private void InitView() {
		refreshLayout = (SwipeRefreshLayout) getActivity().findViewById(
				R.id.picture_refreshLayout);
		refreshLayout.setOnRefreshListener(this);
		refreshLayout.setColorSchemeResources(android.R.color.holo_green_dark,
				android.R.color.holo_green_light,
				android.R.color.holo_orange_light,
				android.R.color.holo_red_light);
		application_gallery = (LinearLayout)getActivity().findViewById(R.id.application_gallery);
		style_gallery = (LinearLayout)getActivity().findViewById(R.id.style_gallery);
		area_gallery = (LinearLayout)getActivity().findViewById(R.id.area_gallery);
		inflater = LayoutInflater.from(getActivity());
	}

	private void InitData(){
		for (int i = 0; i < 10; i++) {
			realPicture = (RelativeLayout)inflater.inflate(R.layout.gallery_view, null);
			application_gallery.addView(realPicture);
			realPicture.setOnClickListener(this);
		}
		
		for (int i = 0; i < 10; i++) {
			realPicture = (RelativeLayout)inflater.inflate(R.layout.gallery_view, null);
			style_gallery.addView(realPicture);
			realPicture.setOnClickListener(this);
		}
		
		for (int i = 0; i < 10; i++) {
			realPicture = (RelativeLayout)inflater.inflate(R.layout.gallery_view, null);
			area_gallery.addView(realPicture);
			realPicture.setOnClickListener(this);
		}
	}
	
	@Override
	public void onRefresh() {
		refreshLayout.setRefreshing(true);
		hander.sendEmptyMessageDelayed(1, 2000);  
	}

	@Override
	public void onClick(View v) {
		Intent intent = new Intent(getActivity(),PictureBookListActivity.class);
		startActivity(intent);
		getActivity().overridePendingTransition(R.anim.slide_in_right, R.anim.slide_out_left);
	}
}
